<?php
/**
 * Languages controller for JA News Component
 * @package Joomlart
 * @subpackage Component
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class JanewsControllerlanguages extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array())
	{
		parent::__construct( $default );

		$this->registerTask( 'apply', 'save' );

		$this->registerTask( 'edit', 'display');
	}

	/**
	 * Display the list of languages
	 */
	function display()
	{		
	  JRequest::setVar( 'view', 'languages'  );
		switch($this->getTask())
		{						
			case 'add':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', false  );
				JRequest::setVar( 'layout', 'form'  );
			} break;	
					
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true  );
				JRequest::setVar( 'layout', 'form'  );
			} break;
		}		
		
		parent::display();
	}

	function cancel(){
		$option = JRequest::getWord('option');
		$client = JRequest::getVar('client', 0);
		$msg = JText::_( 'OPERATION CANCELLED' );
		$this->setRedirect("index.php?option=$option&controller=languages&client=$client", $msg);
	}

	function save(){
		$option = JRequest::getWord('option');
		jimport('joomla.filesystem.file');
		$post	= JRequest::get('post');
		$file = $post['path_lang'].DS.$post['filename'].DS.$post['filename'].'.'.$option.'.ini';		
		JFile::write($file, $post['datalang']);	
		if($this->getTask() == 'apply'){
			$this->setRedirect('index.php?option='.$option.'&controller=languages&task=edit&client='.$post['client'].'&lang='.$post['filename'], JText::_('FILE SAVED'));
		}else{
			$this->setRedirect('index.php?option='.$option.'&controller=languages&client='.$post['client'], JText::_('FILE SAVED'));
		}
	}
}
?>